package cn.zk.obd.client;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.net.SocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
 * Handler
 * 该handler处理客户端发送的信息（打印出来），并发送信息（你好服务端！）给服务端
 */
public class NettyClientHandler extends ChannelInboundHandlerAdapter {
    /**
     * 通道就绪就会触发该方法
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        // 给服务器发消息
        String msg = "7e070403910126301545171d36000601008c000000000000000301d40d9306348c9f01ff025800a1210926143305ea6e0003050800183f310004050b000245cd00050400040749000604001d19cb000704000191a500100e000400fa003400380065004c00940012020089001301000014011b001502000000160140001702010200180109001902020e001a011f001d0101001e0400183f310020020010008c000000000000000301d40ad306348dba01fe024400a0210926143310ea6e0003050800183f8e0004050b000245d10005040004074f000604001d19cb000704000191a500100e000400fa0042006d0044004200ad001202008c001301000014011b00150200000016014000170201020018010b0019020199001a0121001d0101001e0400183f8e0020020010008c000000000000000301d4082806348eca01fc023000a0210926143315ea6e0003050800183fe90004050b000245d500050400040754000604001d19cb000704000191a500100e000400fa005700420023002f0073001202008a001301000014011b00150200000016014000170201020018010a0019020199001a011e001d0101001e0400183fe90020020010008c000000000000000301d4060906348f9e01fb023000a0210926143320ea6e00030508001840300004050b000245d900050400040758000604001d19cb000704000191a500100e000400fa005700430029006700b7001202008a001301000014011b0015020000001601400017020102001801090019020154001a0122001d0101001e04001840300020020010008c000000000000000301d4036d063490ac01fa022600a1210926143325ea6e00030508001840890004050b000245dd0005040004075d000604001d19cb000704000191a500100e000400fa007e00390024003300990012020089001301000014010000150200000016014000170201020018010a0019020169001a011c001d0101001e0400184089002002001000c6000000000000000301d400e5063491a001f8021200a2210926143330ea6e00030508001840de0004050b000245e100050400040762000604001d19cb000704000191a500100e000400fa0048004a004a0043008f001202008b001301000014011700150200000016014000170201020018010d00190200cf001a0123001d0101001e04001840de0020020010eb3860c00206b060d001366050017c60f001286014010060100100610002002061100200006210040000000060400100607002000060e0020280d57e";
        byte[] bytes = msg.getBytes();
        ctx.writeAndFlush(Unpooled.copiedBuffer(bytes));
        System.out.println("发送成功！");
        System.out.println(Arrays.toString(bytes));
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        // 获取服务器的消息
        ByteBuf buf = (ByteBuf) msg;
        String message = buf.toString(StandardCharsets.UTF_8);
        SocketAddress socketAddress = ctx.channel().remoteAddress();
        System.out.println("来自服务器:"+socketAddress+"发送的消息:"+message);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
        // 关闭连接
        ctx.close();
    }
}
